import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class RefreshContent extends StatelessWidget {
  final bool enableRefresh;
  final bool enableLoadMore;
  final Widget child;
  final Function(RefreshController)? onRefresh;
  final Function(RefreshController)? onLoadMore;
  final Function? initData;

  RefreshContent(
      {super.key,
        required this.child,
        this.enableRefresh = true,
        this.enableLoadMore = true,
        this.onRefresh,
        this.onLoadMore,
        this.initData});

  final RefreshController _refreshController =
  RefreshController(initialRefresh: false);

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: _refreshController,
      enablePullDown: enableRefresh,
      enablePullUp: enableLoadMore,
      header: const ClassicHeader(),
      footer: const ClassicFooter(),
      onRefresh: _onRefresh,
      onLoading: _onLoadMore,
      child: child,
    );
  }

  Widget buildHeader(String data) {
    return Text(data);
  }

  _onRefresh() {
    if (onRefresh != null) {
      onRefresh!(_refreshController);
    }
  }

  _onLoadMore() {
    if (onLoadMore != null) {
      onLoadMore!(_refreshController);
    }
    _refreshController.loadComplete();
  }
}
